package com.a16.yuejian.yuejian.model;

import com.a16.yuejian.yuejian.javabean.Exist;
import com.a16.yuejian.yuejian.javabean.Login;
import com.a16.yuejian.yuejian.javabean.Success;
import com.a16.yuejian.yuejian.javabean.User;
import com.a16.yuejian.yuejian.listener.ExistListener;
import com.a16.yuejian.yuejian.listener.LoginListener;
import com.a16.yuejian.yuejian.listener.SuccessListener;
import com.a16.yuejian.yuejian.listener.UserListener;
import com.a16.yuejian.yuejian.service.UserService;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by 55080 on 2017/9/25.
 */

public class UserModel{
    private Retrofit retrofit;
    public UserModel(){
        retrofit =new Retrofit.Builder()
                .baseUrl("http://10.0.2.2:8080/neu_yuejian201/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public void login(String phone, String pwd, final LoginListener listener){
        UserService userService = retrofit.create(UserService.class);
        Call call = userService.login(phone,pwd);
        Callback<Login> callback = new Callback<Login>() {
            @Override
            public void onResponse(Call<Login> call, Response<Login> response) {
                if (response.isSuccessful() && response.body()!=null)
                {
                    listener.onResponse(response.body());
                }
            }

            @Override
            public void onFailure(Call<Login> call, Throwable t) {

            }
        };
        call.enqueue(callback);
    }

    public void reg(String phone, String username, String pwd, String filename,final SuccessListener listener){
        UserService userService = retrofit.create(UserService.class);
        final File file = new File(filename);
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("pic", file.getName(),requestBody);
        Call call = userService.reg(phone,username,pwd,body);
        Callback<Success> callback = new Callback<Success>() {
            @Override
            public void onResponse(Call<Success> call, Response<Success> response) {
                if (response.isSuccessful() && response.body()!=null)
                {
                    listener.onResponse(response.body());
                }
            }

            @Override
            public void onFailure(Call<Success> call, Throwable t) {

            }
        };
        call.enqueue(callback);
    }

    public void exist(String phone,final ExistListener listener){
        UserService userService = retrofit.create(UserService.class);
        Call call = userService.exist(phone);
        Callback<Exist> callback = new Callback<Exist>() {
            @Override
            public void onResponse(Call<Exist> call, Response<Exist> response) {
                if (response.isSuccessful() && response.body()!=null)
                {
                    listener.onResponse(response.body());
                }
            }

            @Override
            public void onFailure(Call<Exist> call, Throwable t) {

            }
        };
        call.enqueue(callback);
    }

    public void userinfo(String uid, final UserListener listener){
        UserService userService = retrofit.create(UserService.class);
        Call call = userService.userinfo(uid);
        Callback<User> callback = new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                if (response.isSuccessful() && response.body()!=null)
                {
                    listener.onResponse(response.body());
                }
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {

            }
        };
        call.enqueue(callback);
    }
}
